﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using UCNKompetenceBors.Database;

namespace UCNKompetenceBors.Login
{
    public partial class signup : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ckeditorBiography.Width = 600;
                this.Form.DefaultButton = btnCreateUser.UniqueID;

                string culture = String.Empty;

                if (HttpContext.Current.Session["MyCulture"] != null)
                    culture = HttpContext.Current.Session["MyCulture"].ToString();

                if (!String.IsNullOrEmpty(culture))
                    GeneralTools.SetCulture(culture);

                lblConfirmCreatedUser.Visible = false;

                LocalizeControls();
            }
        }

        protected void btnCreateUser_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                Membership.CreateUser(txtUserName.Text, txtPassword.Text, txtEmail.Text);

                Roles.AddUserToRole(txtUserName.Text, "user");

                AddAdditionalInfo(txtUserName.Text, txtFirstName.Text, txtLastName.Text, txtPhone.Text, ckeditorBiography.Text);

                panCreateUserForm.Visible = false;
                lblConfirmCreatedUser.Visible = true;
            }
            
        }

        /// <summary>
        /// Adds additional profile information to an already existing user, based on the input parameters.
        /// </summary>
        /// <param name="userName">Specifies the user name to be used.</param>
        /// <param name="firstName">Specifies the first name to be used.</param>
        /// <param name="lastName">Specifies the last name to be used.</param>
        /// <param name="phone">Specifies the phone to be used.</param>
        /// <param name="bio">Specifies the biography to be used.</param>
        private void AddAdditionalInfo(string userName, string firstName, string lastName, string phone, string bio)
        {
            dbLogin.AddProfileInformation(userName, firstName, lastName, phone, bio);
        }

        /// <summary>
        /// Validates the user name.
        /// </summary>
        /// <param name="userName">Specifies the user name to be validted.</param>
        /// <returns>Returns a boolean, specifying if the user name exists or not.</returns>
        private bool ValidateUsername(string userName)
        {
            return (Membership.GetUser(userName) != null);
        }

        //Event fired when the customvalidator validates on the server
        protected void cusvalUserName_ServerValidate(object source, ServerValidateEventArgs args)
        {
            if (ValidateUsername(txtUserName.Text))
            {
                args.IsValid = false;
            }
            else
            {
                args.IsValid = true;
            }
        }

        //Event fired when the custom validator validates on the server
        protected void cusvalEmail_ServerValidate(object source, ServerValidateEventArgs args)
        {
            string username = Membership.GetUserNameByEmail(txtEmail.Text);
            if (!String.IsNullOrEmpty(username))
            {
                args.IsValid = false;
            }
            else
            {
                args.IsValid = true;
            }
        }

        /// <summary>
        /// Localizes the controls of the site, based on the current UI Culture.
        /// </summary>
        private void LocalizeControls()
        {
            btnCreateUser.Text = GetLocalResourceObject("btnCreateUserText").ToString();

            string passwordCompareValidate = GetLocalResourceObject("compvalConfirmPasswordText").ToString();
            compvalConfirmPassword.ToolTip = passwordCompareValidate;
            compvalConfirmPassword.ErrorMessage = passwordCompareValidate;

            string userNameValidityValidate = GetLocalResourceObject("cusvalUserNameText").ToString();
            cusvalUserName.ToolTip = userNameValidityValidate;
            cusvalUserName.ErrorMessage = userNameValidityValidate;

            lblAdditionalInfo.Text = GetLocalResourceObject("lblAdditionalInfoText").ToString();
            lblBiography.Text = GetLocalResourceObject("lblBiographyText").ToString();
            lblConfirmPassword.Text = GetLocalResourceObject("lblConfirmPasswordText").ToString();
            lblFirstName.Text = GetLocalResourceObject("lblFirstNameText").ToString();
            lblLastName.Text = GetLocalResourceObject("lblLastNameText").ToString();
            lblPhone.Text = GetLocalResourceObject("lblPhoneText").ToString();
            lblSignupFormHeader.Text = GetLocalResourceObject("lblSignupFormHeaderText").ToString();
            lblUserName.Text = GetLocalResourceObject("lblUserNameText").ToString();
            lblConfirmCreatedUser.Text = GetLocalResourceObject("lblConfirmCreatedUserText").ToString();

            string emailValidityValidate = GetLocalResourceObject("regexvalEmailText").ToString();
            regexvalEmail.ToolTip = emailValidityValidate;
            regexvalEmail.ErrorMessage = emailValidityValidate;

            string passwordStrengthValidate = GetLocalResourceObject("regexvalPasswordStrengthText").ToString();
            regexvalPasswordStrength.ToolTip = passwordStrengthValidate;
            regexvalPasswordStrength.ErrorMessage = passwordStrengthValidate;

            string phoneValidate = GetLocalResourceObject("regexvalPhoneText").ToString();
            regexvalPhone.ToolTip = phoneValidate;
            regexvalPhone.ErrorMessage = phoneValidate;

            string confirmPasswordValidate = GetLocalResourceObject("rfvalConfirmPasswordText").ToString();
            rfvalConfirmPassword.ToolTip = confirmPasswordValidate;
            rfvalConfirmPassword.ErrorMessage = confirmPasswordValidate;

            string emailValidate = GetLocalResourceObject("rfvalEmailText").ToString();
            rfvalEmail.ToolTip = emailValidate;
            rfvalEmail.ErrorMessage = emailValidate;

            string emailAvailabilityValidate = GetLocalResourceObject("cusvalEmailText").ToString();
            cusvalEmail.ToolTip = emailAvailabilityValidate;
            cusvalEmail.ErrorMessage = emailAvailabilityValidate;

            string fNameValidate = GetLocalResourceObject("rfvalFirstNameText").ToString();
            rfvalFirstName.ToolTip = fNameValidate;
            rfvalFirstName.ErrorMessage = fNameValidate;

            string lNameValidate = GetLocalResourceObject("rfvalLastNameText").ToString();
            rfvalLastName.ToolTip = lNameValidate;
            rfvalLastName.ErrorMessage = lNameValidate;

            string passwordValidate = GetLocalResourceObject("rfvalPasswordText").ToString();
            rfvalPassword.ToolTip = passwordValidate;
            rfvalPassword.ErrorMessage = passwordValidate;

            string userNameValidate = GetLocalResourceObject("rfvalUserNameText").ToString();
            rfvalUserName.ToolTip = userNameValidate;
            rfvalUserName.ErrorMessage = userNameValidate;

            valsumSignupForm.HeaderText = GetLocalResourceObject("valsumSignupFormText").ToString();

            string culture = String.Empty;

            if (HttpContext.Current.Session["MyCulture"] != null)
                culture = HttpContext.Current.Session["MyCulture"].ToString();

            if (culture.Equals("en-GB"))
                ckeditorBiography.Language = "en";
            else
                ckeditorBiography.Language = "da";
        }
    }
}